package com.ym.controller.doctor.work;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ym.common.constant.Constants;
import com.ym.medical.service.IAxbResultSV;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;

/**
 * @author: 张文博
 * @version: 2019年06月19日 13:36
 */
@Api("通话结束回调")
@Controller
@RequestMapping(value = "/doctor/work/phone",headers = Constants.Api.VERSION)
public class HuaWeiPhoneCallbackController {

	private static Logger logger = LoggerFactory.getLogger(HuaWeiPhoneCallbackController.class);

	@Autowired
	private IAxbResultSV iAxbResultSV;

	@ApiOperation("通知接口")
	@PostMapping("/callback")
	@ResponseBody
	public void huaWeiCallback(HttpServletRequest request, HttpServletResponse response) throws Exception {

		BufferedReader in = null;
		try {
			in = new BufferedReader(
					new InputStreamReader(request.getInputStream(), StandardCharsets.UTF_8));
			String line = "";
			StringBuffer stringJson = new StringBuffer();
			while ((line = in.readLine()) != null) {
				stringJson.append(line);
			}
			// 解析json
			JSONObject jsonObject = JSON.parseObject(stringJson.toString());
			iAxbResultSV.paseParams(jsonObject);
		} catch (IOException e) {
			logger.info("华为结束通话回调=============>" + e.getMessage());
		} finally {
			ServletOutputStream outputStream = response.getOutputStream();
			outputStream.write("HTTP/1.1 200 OK".getBytes());
			outputStream.close();
			logger.info("华为结束通话回调=============> 成功");
			if (in != null) {
				in.close();
			}
		}
	}

}
